package com.xiaoyu.merchant.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.utils.UIUtils;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.adapter.upload.GoodsCategoryAdapter;
import com.xiaoyu.merchant.model.GoodsCateGoryBean;
import com.xiaoyu.merchant.model.GoodsCateGoryBean.GoodsCateGoryParam;
import com.xiaoyu.merchant.network.NetworkManager;

import java.util.ArrayList;
import java.util.List;


/**
 * 商品类别选择弹窗
 */
public class ChooseCateGoryDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private TextView commitBtn;//确定按钮
    private ListView mListView;
    private List<GoodsCateGoryParam> mList = new ArrayList<>();
    private String mSelectId = "";//选中的商品类别id
    private GoodsCategoryAdapter mAdapter;
    private OnSelectCategoryListener mListener;
    private GoodsCateGoryParam mSelectParam = null;

    public ChooseCateGoryDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
        this.mContext = context;
        getShopCateGoryList();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_gory);//这行一定要写在前面
        initView();
        setCancelable(true);//点击外部不可dismiss
        setCanceledOnTouchOutside(true);//控制物理返回键是否dismiss
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = UIUtils.dip2px(320);
        window.setAttributes(params);

    }

    private void initView() {
        mListView = findViewById(R.id.dialog_choose_gory_recycler);
        commitBtn = findViewById(R.id.dialog_choose_gory_commit_btn);

        mAdapter = new GoodsCategoryAdapter(mContext,mList);
        mAdapter.setSelectId(mSelectId);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(onItemClickListener);

        commitBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == commitBtn){//确定
            if (mListener != null){
                mListener.onSelected(mSelectParam);
                dismiss();
            }
        }
    }



    /**
     * 类型列表item监听
     *
     *  1、如果点击的item 已经存在选中列表中
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mSelectId = mList.get(position).getGoodstagid();
            mSelectParam = mList.get(position);
            mAdapter.setSelectId(mSelectId);
        }
    };



    //商品类别
    private void getShopCateGoryList() {
        NetworkManager.shopCateGory(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                Log.i("jx","商品类别   onSuccess  result : "+result);
                parseCateSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseCateSuc(String result) {
        GoodsCateGoryBean bean = new Gson().fromJson(result,GoodsCateGoryBean.class);
        mList.clear();
        mList.addAll(bean.getData());
        mAdapter.updateList(mList);
    }


    public void show(GoodsCateGoryParam category){
        if (category != null){
            this.mSelectId = category.getGoodstagid();
            mSelectParam = category;
        }

        show();
    }

    public interface OnSelectCategoryListener{
        void onSelected(GoodsCateGoryParam category);
    }

    public void setOnSelectCategoryListener(OnSelectCategoryListener mListener) {
        this.mListener = mListener;
    }
}
